package com.example.idie.bean;

import com.jcraft.jsch.*;
import java.io.*;
import java.util.Properties;

public class SSHServiceBean {

    private String host;
    private Integer port;
    private String username;
    private String privateKeyPath;


    public SSHServiceBean(String host, int port, String username, String privateKeyPath) {
        this.host = host;
        this.port = port;
        this.username = username;
        this.privateKeyPath = privateKeyPath;
    }

    public String executeCommand(String command) {
        Session session = null;
        Channel channel = null;

        try {
            JSch jsch = new JSch();
            jsch.addIdentity(privateKeyPath);

            session = jsch.getSession(username, host, port);

            Properties config = new Properties();
            config.put("StrictHostKeyChecking", "no");
            session.setConfig(config);

            session.connect();

            channel = session.openChannel("exec");
            ((ChannelExec) channel).setCommand(command);

            channel.setInputStream(null);
            ((ChannelExec) channel).setErrStream(System.err);

            channel.connect();


            // Read the output from the remote host
            InputStream input = channel.getInputStream();
            StringBuilder output_res = new StringBuilder();
            try (BufferedReader reader = new BufferedReader(new InputStreamReader(input))) {
                String line;
                while ((line = reader.readLine()) != null) {
                    System.out.println(line);
                    output_res.append(line).append("\n");
                }
            }

            channel.disconnect();
            session.disconnect();

            return output_res.toString();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null) {
                session.disconnect();
            }
            if (channel != null) {
                channel.disconnect();
            }
        }

        return null;
    }

    public void executeCommandNoWaiting(String command) {
        Session session = null;
        Channel channel = null;

        try {
            JSch jsch = new JSch();
            jsch.addIdentity(privateKeyPath);

            session = jsch.getSession(username, host, port);

            Properties config = new Properties();
            config.put("StrictHostKeyChecking", "no");
            session.setConfig(config);

            session.connect();

            channel = session.openChannel("exec");
            ((ChannelExec) channel).setCommand(command);

            channel.setInputStream(null);
            ((ChannelExec) channel).setErrStream(System.err);

            channel.connect();

            channel.disconnect();
            session.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void executeCommandThread(String command) {
        new Thread(() -> {
            Session session = null;
            Channel channel = null;
            try {
                JSch jsch = new JSch();
                jsch.addIdentity(privateKeyPath);

                session = jsch.getSession(username, host, port);

                Properties config = new Properties();
                config.put("StrictHostKeyChecking", "no");
                session.setConfig(config);

                session.connect();

                channel = session.openChannel("exec");
                ((ChannelExec) channel).setCommand(command);

                channel.setInputStream(null);
                ((ChannelExec) channel).setErrStream(System.err);

                channel.connect();


                // Read the output from the remote host
                InputStream input = channel.getInputStream();
                StringBuilder output_res = new StringBuilder();
                try (BufferedReader reader = new BufferedReader(new InputStreamReader(input))) {
                    String line;
                    while ((line = reader.readLine()) != null) {
                        System.out.println(line);
                        output_res.append(line).append("\n");
                    }
                }

                channel.disconnect();
                session.disconnect();

            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (session != null) {
                    session.disconnect();
                }
                if (channel != null) {
                    channel.disconnect();
                }
            }

        }).start();
    }
}